package com.anda.logistics.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Component;

import com.anda.logistics.dao.SystemRightsDao;
import com.anda.logistics.model.SystemMenuRights;
import com.anda.logistics.util.JQGridPager;
@Component
public class SystemRightsDaoImpl extends AndaBaseSuperDao implements SystemRightsDao {

	@Override
	public List<SystemMenuRights> getPagerList(JQGridPager pager) {
		StringBuffer queryStr = new StringBuffer();
		queryStr.append("from SystemMenuRights ")
			.append(pager.getPagedWhereClause())
			.append(" order by id ");
		
		Query q = sessionFactory.getCurrentSession()
			.createQuery(queryStr.toString());
		q = pager.setPagedParameterSql(q);
		
		//q.setFirstResult((pager.getPage() -1) * pager.getRows());
		//q.setMaxResults(pager.getRows());
		return q.list();
	}

	@Override
	public long getPagerRecords(JQGridPager pager) {
		StringBuffer queryString = new StringBuffer();
		queryString.append("select count(*) from SystemMenuRights ");
		queryString.append(pager.getPagedWhereClause());
		Query q = sessionFactory.getCurrentSession()
				.createQuery(queryString.toString());
		q = pager.setPagedParameterSql(q);
		return (Long) ( q.uniqueResult());
	}

	@Override
	public void save(SystemMenuRights smr) {
		sessionFactory.getCurrentSession().save(smr);		
	}

	@Override
	public void update(SystemMenuRights smr) {
		sessionFactory.getCurrentSession().update(smr);		
	}
	
	@Override 
	public SystemMenuRights getOneById(int id){
		return (SystemMenuRights) sessionFactory.getCurrentSession().get(SystemMenuRights.class, id);
	}
	
	@Override
	public void delete(int id) {		
		sessionFactory.getCurrentSession().delete(getOneById(id));		
	}

	@Override
	public List<SystemMenuRights> getAll() {
		return sessionFactory.getCurrentSession()
				.createQuery("from SystemMenuRights order by id")
				.list();
	}

}
